package com.qf.controller;


import com.qf.utils.PayUtil;
import com.qf.utils.ZxingUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;
import java.util.*;

@Controller
@RequestMapping("/wechatpay")
public class WechatPayController {
    private Random random = new Random();

    @GetMapping(value = "/test")
    @ResponseBody
    public String pay(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {

        List<Map<String,Object>> list=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();

        String money = req.getParameter("num");
        System.out.println("req.getParmenter" + ":" + money);

        int moneys = Integer.parseInt(money) * 100;
        String endMoneys = "" + moneys;

        req.setCharacterEncoding("UTF-8");
        //        String body=req.getParameter("body");//用户要购买的商品
        String body = "java";
        String price = "1";
        String order_id = random.nextInt(10000000) + "";


        //生成二维码
        //1，先得到字符串二维码
        try {
            String payURL = PayUtil.getPayURL(body, order_id, endMoneys);//获取到二维码字符串
            BufferedImage image = ZxingUtil.createImage(payURL, 300, 300);

            req.getSession().setAttribute("image", image);
            req.getSession().setAttribute("oid", order_id);
//            map.put("image",image);
//            map.put("oid",order_id);
//            list.add(map);
            resp.sendRedirect("/front_system_war_exploded/payment.html");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "payment";
    }
}
